﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using KinonekoSoftware.ArkSpark.Runtimes.Presenters;

namespace KinonekoSoftware.ArkSpark.Helpers
{
    public static class TextBoxStatic
    {
        public static readonly DependencyProperty AutoFocusProperty =
            DependencyProperty.RegisterAttached(
                                                "ShadowAutoFocus",
                                                typeof(bool),
                                                typeof(TextBoxStatic),
                                                new PropertyMetadata(default(bool)));

        public static readonly DependencyProperty EnableHeadingTextProperty =
            DependencyProperty.RegisterAttached(
                                                "ShadowEnableHeadingText",
                                                typeof(bool),
                                                typeof(TextBoxStatic), 
                                                new PropertyMetadata(default(bool)));
        
        
        const int D1 = (int)Key.D1;
        const int D7 = (int)Key.D7;
        const int N1 = (int)Key.NumPad1;
        const int N7 = (int)Key.NumPad7;

        public static void SetEnableHeadingText(TextBox element, bool value)
        {
            element.KeyUp += OnKeyUp;
            element.Unloaded += OnUnloaded;
            element.SetValue(EnableHeadingTextProperty, value);
        }
        
        public static bool GetEnableHeadingText(TextBox element)
        {
            return (bool)element.GetValue(EnableHeadingTextProperty);
        }
        
        public static bool GetAutoFocus(TextBox element)
        {
            return (bool)element.GetValue(AutoFocusProperty);
        }
        
        public static void SetAutoFocus(TextBox element, bool value)
        {
            element.Focus();
            element.SetValue(AutoFocusProperty, value);
        }
        
        
        private static void OnKeyUp(object sender, KeyEventArgs e)
        {
            var keyNum = (int)e.Key;
            var level = -1;

            if (e.KeyboardDevice.Modifiers != ModifierKeys.Control)
            {
                return;
            }

            if (keyNum >= D1 && keyNum <= D7)
            {
                level = Math.Clamp(keyNum - D1 + 1, 1, 7);
            }

            if (keyNum >= N1 && keyNum <= N7)
            {
                level = Math.Clamp(keyNum - N1 + 1, 1, 7);
            }

            if(level < 0)
            {
                return;
            }

            if (sender is TextBox { DataContext: HeadingTextPresenter presenter })
            {
                presenter.Level = level;
                presenter.Update();
            }
        }


        private static void OnUnloaded(object sender, RoutedEventArgs e)
        {
            if (sender is TextBox editor)
            {
                editor.KeyUp    -= OnKeyUp;
                editor.Unloaded -= OnUnloaded;
            }
        }


    }
}